import api from './api'
const apiUrl = cms.root + 'home.php/'

const AJAX = ({ url = '', method = 'get', data = {}, headers = {} }) => {
  if (url === 'verify' || url === 'snslogin') {
    return url === 'snslogin' ? apiUrl + '/user/snslogin/' + data.type + '/t/1' : apiUrl + api[url] + '/' + data.s
  }
  let option = { url: api[url].indexOf('http') >= 0 ? api[url] : apiUrl + api[url], method, headers }
  data._t = new Date().getTime()
  option.data = data

  return $.ajax(option)
    .then((res) => {
      return res
    })
    .fail((error) => {
      return [error]
    })
}

export default AJAX
